// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Globalization;
using System.Web.UI.WebControls;
using BlueOnionSoftware.Bloget.Properties;

namespace BlueOnionSoftware.Bloget
{
    internal class CategoriesView : View
    {
        private bool _disposed;
        private TextBox _addCategory;

        internal CategoriesView(Bloget bloget)
            : base(bloget)
        {
        }

        protected override void Dispose(bool managed)
        {
            try
            {
                if (_disposed == false)
                {
                    _disposed = true;

                    if (managed)
                    {
                        _addCategory.Dispose();
                    }
                }
            }

            finally
            {
                base.Dispose(managed);
            }
        }

        protected override string RootClassId()
        {
            return "categories-view";
        }

        protected override void ShowView()
        {
            if (Bloget.IsLoggedIn == false || Bloget.IsAdministrator == false)
            {
                var passwordForm = new PasswordView(Bloget);
                passwordForm.Show();
                return;
            }

            MainMenu.ShowMenu(Bloget);

            _addCategory = new TextBox {MaxLength = 25};
            Bloget.Controls.Add(_addCategory);

            Bloget.Write("&nbsp;");

            var addButton = new Button {Text = Resources.CategoriesAdd};
            addButton.Command += AddButtonCommand;
            Bloget.Controls.Add(addButton);
            Bloget.WriteLine("<br/><br/>");

            foreach (var category in Bloget.Blog.Categories.Sorted())
            {
                CategoryItem(category);
            }
        }

        private void CategoryItem(Category category)
        {
            if (category == null)
            {
                throw new ArgumentNullException("category");
            }
            var delete = new ConfirmedButton
            {
                Text = Resources.CategoriesDelete,
                Message = string.Format(CultureInfo.CurrentCulture, Resources.CategoriesConfirmDelete, category)
            };
            delete.Command += DeleteCommand;
            delete.CommandArgument = category.Id.ToString(CultureInfo.InvariantCulture);
            Bloget.Controls.Add(delete);

            Bloget.WriteLine("&nbsp;");

            var edit = new Button {Text = Resources.CategoriesEdit};
            edit.Command += EditCommand;
            var textBoxId = "C" + category.Id.ToString(CultureInfo.InvariantCulture);
            edit.CommandArgument = textBoxId;
            Bloget.Controls.Add(edit);

            Bloget.WriteLine("&nbsp;");

            var textBox = new CategoryTextBox
            {
                ID = textBoxId,
                CategoryId = category.Id,
                Text = category.Name,
                MaxLength = 25
            };
            Bloget.Controls.Add(textBox);

            Bloget.WriteLine("&nbsp;");
        }

        private void AddButtonCommand(object sender, CommandEventArgs e)
        {
            if (string.IsNullOrEmpty(_addCategory.Text) == false)
            {
                Bloget.Blog.Categories.CreateCategory(_addCategory.Text);
                Bloget.Redirect(Bloget.Page.Request.RawUrl);
            }
        }

        private void DeleteCommand(object sender, CommandEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            int categoryId;

            if (int.TryParse(e.CommandArgument as string, out categoryId))
            {
                if (Bloget.Blog.Categories.Contains(categoryId))
                {
                    var category = Bloget.Blog.Categories[categoryId];
                    category.Deleted = true;
                    Bloget.Redirect(Bloget.Page.Request.RawUrl);
                }
            }
        }

        private void EditCommand(object sender, CommandEventArgs e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            var textBox = Bloget.FindControl(e.CommandArgument as string) as CategoryTextBox;

            if (textBox != null)
            {
                if (string.IsNullOrEmpty(textBox.Text) == false)
                {
                    if (Bloget.Blog.Categories.Contains(textBox.CategoryId))
                    {
                        var category = Bloget.Blog.Categories[textBox.CategoryId];
                        category.Name = textBox.Text;
                    }
                }
            }
        }
    }

    internal class CategoryTextBox : TextBox
    {
        internal int CategoryId { get; set; }
    }
}